package com.lijinjiang.jdbc;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @ClassName Demo01
 * @Description TODO
 * @Author Li
 * @Date 2022/9/22 8:42
 * @ModifyDate 2022/9/22 8:42
 * @Version 1.0
 */
public class Demo01 {
    @Test
    public void TestMethod() throws Exception {
        // 1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2.获取连接
        String url = "jdbc:mysql://localhost:3306/db01?useSSL=false&useServerPrepStmts=true";
        String user = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url, user, password);
        // 3.定义SQL
        String sql = "SELECT * FROM tb01";
        // 4.获取执行SQL对象
        PreparedStatement prepStmt = conn.prepareStatement(sql);
        // 5.执行SQL，获得结果
        ResultSet rs = prepStmt.executeQuery();
        // 6.处理结果
        while (rs.next()) {
            String id = rs.getString(1);
            String value = rs.getString(2);
            System.out.println(id + "→" + value);
        }
        // 7.关闭资源
        rs.close();
        prepStmt.close();
        conn.close();

    }
}
